package data.content;

import java.io.IOException;

import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import stalker.Stalker;
import stalker.StalkerParser;
import data.content.visitor.ContentVisitor;

/**
 * Content that describes a user joins the room
 */

public class ContentStalkerLeaves implements Content {
  
  public final Stalker stalker_;
  
  /**
   * Rep invariant
   */
  private void checkRep() {
    assert (stalker_ != null) :
      "ContentUserJoin: stalker_ must not be null";
  }
  
  /**
   * Creates a new stalker leaves event
   * @requires stalker not null
   * @param stalker stalker who leaves
   * @complexity A(1)
   */
  public ContentStalkerLeaves(Stalker stalker) {
    assert stalker != null;
    
    stalker_ = stalker;
    checkRep();
  }
  
  /**
   * Decodes a stalker join event from the net
   * @requires reader not null
   * @param reader the net
   * @throws IOException 
   */
  public ContentStalkerLeaves(FrameReader reader) throws IOException {
    assert reader != null;
    
    stalker_ = StalkerParser.retrieve(reader);
    
    checkRep();
  }
  
  /**
   * Retrieves the stalker of this entity
   * @return the stalker represented by this object
   * @complexity A(1)
   */
  public Stalker getStalker() {
    return stalker_;
  }
  
  @Override
  public String toString() {
    return stalker_.toString();
  }
  
  /**
   * Equal iff the strings represented are equal
   * @param  obj the object to be compared with
   * @return true iff the strings represented are equal
   * @complexity A(1)
   */
  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof ContentStalkerLeaves)) return false;
    ContentStalkerLeaves that = (ContentStalkerLeaves)obj;
    return this.getStalker().equals(that.getStalker());
  }
  
  public <T> T accept(ContentVisitor<T> visitor) {
    assert visitor != null;
    return visitor.visit(this);
  }

  public void send(FrameWriter writer) throws IOException {
    assert writer != null;
    StalkerParser.send(this.getStalker(), writer);
  }

}
